package com.xpec.c4.game.zone;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.lobby.GameLobbyProcesser;
import com.ms.player.Player;
import com.ms.service.module.zone.LoginZoneGetPlayerDataFromDCParam;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.game.datamanager.agent.PlayerData;
import com.xpec.c4.game.datamanager.agent.PlayerDataAgent;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.ConstantCode;
import com.xpec.c4.service.module.BasicServiceModule;

/**
 * zone 裡面接收玩家資料用的 data agent
 * 
 * @author TW15010
 */
public class PlayerDataAgentZone extends PlayerDataAgent {
	Log log = LogFactory.getLog(PlayerDataAgentZone.class);

	public PlayerDataAgentZone(BasicServiceModule module) {
		super(module);
	}

	public void changeNameUpdatePlayerDataToDCCallback(BaseDataInfo obj, Object... userObject) {
		super.queryPlayerDataCallback(obj, userObject);

		GUID sessiondId = (GUID) userObject[0];

		switch (obj.getResultCode()) {
		case ResultNo.OK: {
			if (sessiondId != null) {
				GameLobbyProcesser.onChangeNameUpdatePlayerDataToDCCallback(true, sessiondId);
			}
			return;
		}

		case ResultNo.ACCESS_DENY:
			log.info("Access deny  id: " + obj.getKey());
			break;

		case ResultNo.FAILED:
			log.info("Query failed  id: " + obj.getKey());
			break;

		case ResultNo.DATA_NOT_FOUND:
			log.info("Data not found id: " + obj.getKey());
			break;

		case ResultNo.DB_OPERATION_FAILED:
			log.info("DB operation failed id: " + obj.getKey());
			break;
		}

		GameLobbyProcesser.onChangeNameUpdatePlayerDataToDCCallback(false, sessiondId);
	}

	public void LoginZoneGetPlayerDataFromDCCallback(BaseDataInfo obj, Object... userObject) {
		super.queryPlayerDataCallback(obj, userObject);

		PlayerData q = (PlayerData) obj;
		Object object = userObject[0];

		Player player = null;
		LoginZoneGetPlayerDataFromDCParam param = null;
		if (object != null && object instanceof LoginZoneGetPlayerDataFromDCParam) {
			player = (Player) obj.getData();
			param = (LoginZoneGetPlayerDataFromDCParam) object;
		}

		switch (obj.getResultCode()) {
		case ResultNo.OK:
			log.info("Load player from dc success, id: " + obj.getKey());
			break;

		case ResultNo.ACCESS_DENY:
			log.info("Access deny  id: " + obj.getKey());
			break;

		case ResultNo.FAILED:
			log.info("Query failed  id: " + obj.getKey());
			break;

		case ResultNo.DATA_NOT_FOUND:
			log.info("Data not found id: " + obj.getKey());
			break;

		case ResultNo.DB_OPERATION_FAILED:
			log.info("DB operation failed id: " + obj.getKey());
			break;

		default:
			log.info(ConstantCode.getName(ResultNo.class, obj.getResultCode()) + " id: " + obj.getKey());
			break;
		}

		// 將取回的玩家資料轉給 zone
		param.getBasicZone().onLoginZoneGetPlayerDataFromDC(q.getKey(), player, param.getSessionId());
	}
}
